<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<div class="button_area">
	<a class="submit button">保存</a>&nbsp;&nbsp;
	<a href="role/role.action" class="button">返回</a>
</div>
<s:form method="post" action="role.update" namespace="/role" >
	<tr>
		<td>
			<s:hidden name="role.roleId"></s:hidden>
			<table width="100%" border="0" align="center" cellpadding="0"
				cellspacing="1" class="form_table">

				<s:textfield label="角色名称" name="role.roleName" size="18"
					required="true" maxlength="20"></s:textfield>

				<tr>
					<td colspan="4">
						&nbsp;
					</td>
				</tr>


				<s:checkbox label="是否为管理员" name="role.admin" id="isAdmin"></s:checkbox>

				<tr>
					<td colspan="4" align="left">
						请选择功能：
					</td>
				</tr>
				<tr>
					<td colspan="4">
						&nbsp;
					</td>
				</tr>
				<tr>

					<td align="left" valign="top">
						<s:subset source="functions" start="0" count="1">
							<s:iterator>
								<span style="font-weight: bold;"><s:property
										value="functionName" /> </span>
								<br>
								<s:iterator value="children">
									<input type="checkbox" name="permission"
										id="<s:property value="functionId" />"
										value="<s:property value="functionId" />">
									<s:property value="functionName" />
									<br>
								</s:iterator>
							</s:iterator>
						</s:subset>
						<br>
						<s:subset source="functions" start="1" count="1">
							<s:iterator>
								<span style="font-weight: bold;"><s:property
										value="functionName" /> </span>
								<br>
								<s:iterator value="children">
									<input type="checkbox" name="permission"
										id="<s:property value="functionId" />"
										value="<s:property value="functionId" />">
									<s:property value="functionName" />
									<br>
								</s:iterator>
							</s:iterator>
						</s:subset>
					</td>
					<td align="left" valign="top">
						<s:subset source="functions" start="2" count="1">
							<s:iterator>
								<span style="font-weight: bold;"><s:property
										value="functionName" /> </span>
								<br>
								<s:iterator value="children">
									<input type="checkbox" name="permission"
										id="<s:property value="functionId" />"
										value="<s:property value="functionId" />">
									<s:property value="functionName" />
									<br>
								</s:iterator>
							</s:iterator>
						</s:subset>
					</td>
					<td width="50%"></td>
				</tr>
			</table>
		</td>
	</tr>
</s:form>
<script type="text/javascript">
$(document).ready(function(){
<s:iterator value="role.permissions">
	$('#<s:property value="function.functionId"/>').attr("checked","checked");
</s:iterator>

function checkAllListener(){
	var isChecked=$('#isAdmin').attr('checked');
	
	$('input[name="permission"]').each(function(){
				if(isChecked){
					this.checked=isChecked;
				}
				this.disabled=isChecked;
	});
}

$('#isAdmin').click(checkAllListener);

checkAllListener();


 $("form").validate({
    	errorLabelContainer: $("div.error"),
    	focusInvalid: true,
    	rules: {           //定义验证规则,其中属性名为表单的name属性  
        	'role.roleName': {  
        		required: true,  
               	rangelength: [2, 50]//,
              	}
         }, 
         messages: {       //自定义验证消息  
         	'role.roleName': {  
            	required: "角色名称是必需的<br>",  
                rangelength: $.format("用户ID至少要{0}个字符<br>")
         	}
         	
         }
    });
});
</script>
